package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Quality;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 2017/3/7.
 */

public class RectificationListAdapter extends BaseAdapter {
    private List<Quality> datas;
    private Context context;

    public RectificationListAdapter(List<Quality> datas, Context context) {
        this.context = context;
        this.datas = datas;
    }

    public void setDatas(List<Quality> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_security, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Quality quality = datas.get(position);
        holder.stateRl.setVisibility(View.GONE);
        holder.divider.setVisibility(View.GONE);
        holder.dataTv.setText(quality.getBusinessDate());
        holder.content.setText(quality.getItems());
        holder.personLiableTv.setText(quality.getContactName());
        holder.personTv.setText(quality.getCreateByName());
        if(quality.getProjectCompanyName() != null && !Utils.isEmpty(quality.getProjectCompanyName())){
            if(quality.getProjectCompanyName().length() > 12){
                holder.companyTv.setText(quality.getProjectCompanyName().substring(0,11) + "...");
            }else{
                holder.companyTv.setText(quality.getProjectCompanyName());
            }
        }else {
            holder.companyTv.setText("无施工单位");
        }

//        if(quality.getProjectStructureName() == null || Utils.isEmpty(quality.getProjectStructureName())){
//            holder.structureTv.setText("无受检部位");
//        }else{
//            holder.structureTv.setText(quality.getProjectStructureName());
//        }

        switch (quality.getCorrectiveStatus()) {
            case 1:
                holder.stateTv.setText("待整改");
                holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_1294db));
                holder.stateIv.setImageResource(R.mipmap.daizhenggai);
                break;
            case 2:
                holder.stateTv.setText("待复检");
                holder.stateIv.setImageResource(R.mipmap.pending_review);
                holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_978cff));
                break;
            case 3:
                holder.stateTv.setText("复检不通过");
                holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_ed8d70));
                holder.stateIv.setImageResource(R.mipmap.canceled);
                break;
            case 4:
                holder.stateTv.setText("已整改");
                holder.stateTv.setTextColor(context.getResources().getColor(R.color.color_71e5a2));
                holder.stateIv.setImageResource(R.mipmap.already_confirmed);
                break;
        }

        holder.orderNumTv.setVisibility(View.VISIBLE);
        holder.orderNumTv.setText(quality.getOrderNo());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.state_rl)
        RelativeLayout stateRl;
        @BindView(R.id.state_iv)
        ImageView stateIv;
        @BindView(R.id.state_tv)
        HXTextView stateTv;
        @BindView(R.id.divider)
        View divider;
        @BindView(R.id.content)
        HXTextView content;
        @BindView(R.id.person_liable_tv)
        HXTextView personLiableTv;
        @BindView(R.id.person_tv)
        HXTextView personTv;
        @BindView(R.id.company_tv)
        HXTextView companyTv;
        @BindView(R.id.data_tv)
        HXTextView dataTv;
        @BindView(R.id.order_num_tv)
        HXTextView orderNumTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
